package com.mongodb.application.test;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.net.ssl.SSLContext;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;

import com.aliyun.configuration.ConfigurationTool;
import com.certificate.tool.SetTrustStore;
import com.common.constant.Constant;

public class CreateMongodbAppForWangyi {
	
	public static void main(String[] args) {
	
		 try {
			 SSLConnectionSocketFactory sslsf = SetTrustStore.getSSLConnection();
			 //httpclient链接
			 CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			 
			 HttpPost httpPost = new HttpPost(ConfigurationTool.getProperties(Constant.ALIYUN_PROJECTS_URL));
			 
			 httpPost.addHeader("Content-type","application/json;charset=UTF-8");
			 
			 StringBuffer composeYaml = new StringBuffer();
			 
			 composeYaml.append(ConfigurationTool.getProperties(Constant.MONGODB_MASTER_CONTAINER_NAME))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_IMAGE))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_MASTER_HOSTNAME))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_PORTS))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_ENVIRONMENT))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_RESTART))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_VOLUME_A)+ConfigurationTool.getProperties(Constant.MONGODB_VOLUME_MASTER_NAME))
				.append(ConfigurationTool.getProperties(Constant.MONGODB_VOLUME_B))
				.append("  "+ConfigurationTool.getProperties(Constant.MONGODB_OTHER_ARGS))
				.append(ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
				;
			 
			 String  requestEntity =
					 "{\"name\":"+"\""+"MongodbTest"+"\","
					 
					+ "\"description\":"+"\""+"test"+"\","
					
					+ "\"template\":"+"\""+composeYaml.toString()+"\""+","
					
					+ "\"version\":\"1.0\"}";
			 
			 StringEntity stringEntity = new StringEntity(requestEntity, Charset.forName("UTF-8"));
			 
			 httpPost.setEntity(stringEntity);

			 //使用客户端向服务器发送数据
			 HttpResponse httpResponse = httpClient.execute(httpPost);
			 
			 if (httpResponse.getStatusLine().getStatusCode() == 201) {
				
				System.out.println(httpResponse.getProtocolVersion()
						+ " " + httpResponse.getStatusLine().getStatusCode()
						+ " " + httpResponse.getStatusLine().getReasonPhrase());
				
				System.out.println("OK:200");
			} else {
				HttpEntity resultEntity = httpResponse.getEntity();
				
				//返回结果是，服务器上，返回来的是json字符串数据
				String strResultEntity = EntityUtils.toString(resultEntity);
				
				System.out.println(strResultEntity);
				
				System.out.println(httpResponse.getStatusLine().getStatusCode());
				
				System.out.println("Something is wrong!!!");
			}
		
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
}
		
		
		
